﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using DcsGauges.Shared.Network;
using DcsGauges.Shared.Packets;

namespace DcsGauges.Server.Network
{
    class DcsGaugesChannel : IDcsGaugesClientChannel
    {
        #region Fields & Properties
        
        public string               ClientName                      { get; private set; }
        private NetworkChannel      networkChannel                  = null;

        #endregion


        #region Events

        public event EventHandler<PacketReceivedEventArgs<CommandsPacket>>      CommandsPacketReceivedEvent;

        #endregion


        #region Constructors

        public DcsGaugesChannel(Socket socket, string clientName)
        {
            this.networkChannel                                 = new NetworkChannel(socket);
            this.ClientName                                     = clientName;
            this.networkChannel.CommandsPacktReceivedEvent      += new EventHandler<PacketReceivedEventArgs<CommandsPacket>>(networkChannel_CommandsPacktReceivedEvent);
            
        }

        #endregion


        #region Event Handlers

        private void networkChannel_CommandsPacktReceivedEvent(object sender, PacketReceivedEventArgs<CommandsPacket> e)
        {
            this.FireCommandsPacketReceived(e);
        }

        #endregion


        #region Operations

        public void Send(Shared.Packets.Packet packet)
        {
            this.networkChannel.Send(packet);
        }

        #endregion


        #region Helpers

        private void FireCommandsPacketReceived(PacketReceivedEventArgs<CommandsPacket> args)
        {
            if (this.CommandsPacketReceivedEvent != null)
            {
                this.CommandsPacketReceivedEvent(this, args);
            }
        }

        #endregion
    }
}
